﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// TMS司机账号角色创建
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMSDriverRoleCreateController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public TMSDriverRoleCreateController(ILogger<TMSDriverRoleCreateRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            _logger = loggerFactory.CreateLogger<TMSShipping_PlanCreateController>();

        }

        /// <summary>
        /// TMS司机账号角色创建
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostDriverRoleCreate([FromBody] TMSDriverRoleCreateRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            TMSDriverRoleCreateResponseDto re = new TMSDriverRoleCreateResponseDto();
            string telephone = strparm.telephone;
            string carno = strparm.carno;
            string times = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();

            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            try
            {
                if (strMsg == "")
                {
                    dic = new Dictionary<string, string>();
                    dic.Add("telephone", telephone);
                    DataTable check_ccode = MethodHelp.GetTableInfo("TMS0058", dic, con);//查询手机号对应系统账号
                    if (check_ccode.Rows.Count > 0)
                    {
                        dic = new Dictionary<string, string>();
                        dic.Add("group_id", times);
                        dic.Add("group_name", times);
                        dic.Add("user_id", check_ccode.Rows[0]["id"].ToString());
                        dic.Add("id", times);
                        dic.Add("carno", check_ccode.Rows[0]["carno"].ToString());
                        strMsg = MethodHelp.InsertIntoTable("TMS0059", dic, con);                       
                    }
                    else
                    {


                        strMsg = "找不到手机号<"+ telephone + ">对应的账号信息，请联系管理员！";

                    }               
                }               
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
            if (strMsg=="")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }

            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);
        }

      
    }
}
